//
//  CalendarViewController.m
//  Calendar
//
//  Created by 张凡 on 14-8-21.
//  Copyright (c) 2014年 张凡. All rights reserved.
//

#import "CalendarViewController.h"
#import "CalendarMonthCollectionViewLayout.h"
#import "CalendarMonthHeaderView.h"
#import "CalendarDayCell.h"
#import "CalendarDayModel.h"
#import "UIViewController+BarItem.h"

@interface CalendarViewController () <UICollectionViewDataSource,UICollectionViewDelegate>


@property (nonatomic ,strong) CalendarLogic *logic;

@end

@implementation CalendarViewController

static NSString *MonthHeader = @"MonthHeaderView";
static NSString *DayCell = @"DayCell";


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
 
        [self initData];
        [self initView];

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
     self.navigationItem.title = @"选择日期";
    [self setLeftBtnItem];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(void)ajustFrame:(CGRect)frame{
    self.collectionView.frame = frame;
}
- (void)initView{
    
    CalendarMonthCollectionViewLayout *layout = [CalendarMonthCollectionViewLayout new];
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout]; //初始化网格视图大小
    [self.collectionView registerClass:[CalendarDayCell class] forCellWithReuseIdentifier:DayCell];//cell重用设置ID
    [self.collectionView registerClass:[CalendarMonthHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:MonthHeader];
    self.collectionView.alwaysBounceVertical = YES;
    self.collectionView.delegate   = self;
    self.collectionView.dataSource = self;
    
    self.collectionView.backgroundColor = [UIColor colorWithRed:241.0/255.0 green:241.0/255.0 blue:241.0/255.0 alpha:1];
    [self.view addSubview:self.collectionView];
}

-(void)initData{
    self.calendarMonth = [[NSMutableArray alloc]init];
}

#pragma mark - CollectionView代理方法

//定义展示的Section的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.calendarMonth.count;
}

//定义展示的UICollectionViewCell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSMutableArray *monthArray = [self.calendarMonth objectAtIndex:section];
    return monthArray.count;
}

//每个UICollectionView展示的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CalendarDayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:DayCell forIndexPath:indexPath];
    NSMutableArray *monthArray = [self.calendarMonth objectAtIndex:indexPath.section];
    CalendarDayModel *model = [monthArray objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    if (kind == UICollectionElementKindSectionHeader){
        NSMutableArray *month_Array = [self.calendarMonth objectAtIndex:indexPath.section];
        CalendarDayModel *model = [month_Array objectAtIndex:15];
        CalendarMonthHeaderView *monthHeader = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:MonthHeader forIndexPath:indexPath];
        monthHeader.masterLabel.text = [NSString stringWithFormat:@"%lu年 %lu月",(unsigned long)model.year,(unsigned long)model.month];
        monthHeader.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8f];
        reusableview = monthHeader;
    }
    return reusableview;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *month_Array = [self.calendarMonth objectAtIndex:indexPath.section];
    CalendarDayModel *model = [month_Array objectAtIndex:indexPath.row];
    if (model.style == CellDayTypeShowPrice  ||model.style == CellDayTypeClick) {
       
        
        if (self.selectedEnable && model.ticketStyle != 1 && model.ticketStyle != 5) {
            return;
        }
        
        
        if (self.isDetail &&  model.ticketStyle != 3) {
            return;
        }
        
        [self.logic selectLogic:model];
        if (self.calendarblock) {
            self.calendarblock(model);
        }
        [self.collectionView reloadData];
    }
}

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

-(void)responseDayModels:(NSMutableArray *)responseDayModels{
    self.logic = [[CalendarLogic alloc] init];
    self.calendarMonth = [self.logic setResponseDayModels:responseDayModels];
    [self.collectionView reloadData];
}

-(void)setSelectedDate:(NSArray *)selectedDateStrings{
    for (NSMutableArray *array in self.calendarMonth) {
        for (CalendarDayModel *calendarDay in array) {
            for (NSString *selecteKey in selectedDateStrings) {
                if ([calendarDay.toString isEqualToString:selecteKey] && calendarDay.style != CellDayTypeEmpty) {
                    calendarDay.style = CellDayTypeClick;
                }
            }
        }
    }
}


@end
